from PySide import QtGui, QtCore
import g_items

class View(QtGui.QGraphicsView):
	
	def __init__(self, parent):
		super(View, self).__init__()
		self.parent = parent
		#self.currentcenter = QtCore.QPoint(0, 0)
		#self.centerOn(400, 400)
		self.scene = QtGui.QGraphicsScene()
		#self.createOrigin()
		self.createMembers()
		self.createNodes()
		self.setScene(self.scene)
		#self.setDragMode(self.ScrollHandDrag)
		self.show()
	
	#def createOrigin(self):
		#self.origin = g_items.OriginItem()
		#self.scene.addItem(self.origin)
	
	def createMembers(self):
		self.members = {}
		for member in self.parent.data.members.itervalues():
			self.addMember(member)
	
	def createNodes(self):
		self.nodes = {}
		for node in self.parent.data.nodes.itervalues():
			self.addNode(node)
	
	def updateScene(self):
		#self.origin.setVisible(self.parent.data.settings.showorigin)
		for member in self.members.values():
			member[0].setVisible(self.parent.data.settings.showmembers)
			member[1].setVisible(self.parent.data.settings.showmembernames)
		for node in self.nodes.values():
			node[0].setVisible(self.parent.data.settings.shownodes)
			node[1].setVisible(self.parent.data.settings.shownodenames)
	
	def addNode(self, node):
		temp = []
		temp.append(g_items.NodeItem(node))
		temp.append(g_items.NodeNameItem(node))
		self.scene.addItem(temp[0])
		self.scene.addItem(temp[1])
		self.nodes[node] = temp
	
	def deleteNode(self, node):
		temp = self.nodes[node]
		self.scene.removeItem(temp[0])
		self.scene.removeItem(temp[1])
		del self.nodes[node]
	
	def addMember(self, member):
		temp = []
		temp.append(g_items.MemberItem(member))
		temp.append(g_items.MemberNameItem(member))
		self.scene.addItem(temp[0])
		self.scene.addItem(temp[1])
		self.members[member] = temp
	
	def deleteMember(self, member):
		temp = self.members[member]
		self.scene.removeItem(temp[0])
		self.scene.removeItem(temp[1])
		del self.members[member]
	
	def updateMember(self, member):
		self.members[member][0].updateMember()
		self.members[member][1].updateMember()
		
	def updateNode(self, node):
		self.nodes[node][0].updateNode()
		self.nodes[node][1].updateNode()
